<!DOCTYPE html>
<html>
<head>
    <title data-culture="packageManagment">包管理</title>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-lg-12 col-sm-12 col-xs-12">
            <div class="widget  no-padding">
                <div class="widget-header bg-themeprimary">
                    <i class="widget-icon typcn typcn-th-list"></i>
                    <span class="widget-caption" data-culture="packageList">包列表</span>
                </div>
                <div class="widget-body">
                    <table id="grid-data">
                        <thead>
                        <tr>
                            <th style="text-align: center;" data-culture="packageName">包名称</th>
                            <th style="text-align: center;" data-culture="packageImgUrl">图片</th>
                            <th style="text-align: center;" data-culture="remark">备注</th>
                            <th style="text-align: center;" data-culture="packageImgUrl">排序</th>
                            <th style="width: 200px; text-align: center;" data-culture="operation">操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr class="row-no-display">
                            <td style="text-align: center;">$packageName$</td>
                            <td render="icon" style="text-align: center; "></td>
                            <td style="text-align: center;">$remark$</td>
                            <td style="text-align: center;">$sort$</td>
                            <td class="text-left" style="text-align: center">
                                <a rel='editPackage' class="btn btn-info btn-xs edit"><i
                                        class="fa fa-edit"></i><span data-culture="edit">编辑</span></a>
                                <a rel='deletePackage' class="btn btn-danger btn-xs edit"><i
                                        class="fa fa-times"></i><span data-culture="delete">删除</span></a>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade form" id="editmodal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">
    <div class="modal-dialog">
        <form id="packageEditForm" method="post" class="form-horizontal" role="form">
            <div class="modal-content">
                <div class="widget no-padding">
                    <div class="widget-header bg-themeprimary">
                        <i class="widget-icon fa fa-user"></i>
                        <span class="widget-caption">包管理</span>
                        <div class="widget-buttons">
                            <a tabindex="7" data-type="reset"><i class="fa fa-refresh"></i></a>
                            <a data-dismiss="modal">
                                <i class="fa fa-times"></i>
                            </a>
                        </div>
                    </div>
                    <div class="widget-body">
                        <table class="table table-bordered form">
                            <thead>
                            <tr>
                                <th style="width:120px;"></th>
                                <td style="width: auto;"></td>
                            </tr>
                            </thead>
                            <tbody>
                            <tr>
                                <th><label for="packageName">包名称</label>
                                </th>
                                <td><input class="form-control" name="packageName" id="packageName" type="text"
                                           placeholder="包名称"/></td>
                            </tr>
                            <tr>
                                <th><label for="imgurl">图片路径</label>
                                </th>
                                <td><input class="form-control" name="imgurl" id="imgurl" type="text"
                                           placeholder="图片路径"/></td>
                            </tr>
                            <th><label for="sort">排序</label>
                            </th>
                            <td><input class="form-control" name="sort" id="sort" type="text"
                                       placeholder="排序"/></td>
                            </tr>
                            <tr>
                                <th><label for="remark">备注</label></th>
                                <td><input type="text" class="form-control" name="remark" id="remark"
                                           placeholder="备注">
                                </td>
                            </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" id="btnSave" class="btn btn-success" data-loading-text="保存中...">
                        <i class="glyphicon glyphicon-floppy-disk"></i>保存
                    </button>
                    <button type="button" class="btn btn-danger" data-dismiss="modal">
                        <i class="glyphicon glyphicon-remove"></i>关闭
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<script type="text/javascript">
    var packageObj = {
        insert: true,
        currentPackage: null,
        validateParam: {
            submitHandler: function (obj) {
                obj.insert = packageObj.insert;
                if (!packageObj.insert) {
                    obj.id = packageObj.currentPackage.id;
                }
                $.request('/workflow/package/save', obj, function (result) {
                    try {
                        $('#editmodal').modal('hide');
                        packageObj.grid.ext.refresh();
                    }
                    catch (ex) {
                    }
                }).always(function () {
                    $('#btnSave').button('reset');
                });
            },
            fields: {
                packageName: {
                    validators: {
                        notEmpty: {}
                    }
                },
                sort: {
                    validators: {
                        notEmpty: {},
                        dataType: {
                            dataType: 'decimal',
                            message: '只能输入数字'
                        }
                    }
                }

            }
        },
        grid: {
            keys: ['packageName'],
            url: "/workflow/package/query/page",
            placeholder: '包名称',
            stableparm: function () {
                return {};
            },
            events: {
                icon:function(row){
                    return "<span class='"+row.imgurl+"'></span>";
                },
                editPackage: function (row) {
                    packageObj.insert = false;
                    packageObj.currentPackage = row;
                    packageObj.validateParam.resetBtn.trigger("reset");
                    $.setobject(row, 'packageEditForm');
                    $('#editmodal').modal('show');
                },
                mappingFunction: function (row) {
                    initFunction(row.id);
                    $('#functionTreeModal').modal('show');
                },
                mappingUser: function (row) {
                    initUser(row.id);
                    $('#userTreeModal').modal('show');
                },
                deletePackage: function (row) {
                    bootbox.confirm('确认删除？', function () {
                        deleteRowList([id = row.id]);
                    });
                }

            },
            tools: [{
                text: '新增', 'data-culture': 'add', img: 'fa fa-plus', event: function (selectItems) {
                    packageObj.insert = true;
                    packageObj.validateParam.resetBtn.trigger("reset");
                    $('#packageName').val('');
                    $('#remark').val('');
                    $('#editmodal').modal('show');
                }
            }, {
                text: '编辑', 'data-culture': 'edit', img: 'fa fa-edit', event: function (selectItems) {
                    if (selectItems && selectItems.length == 1) {
                        packageObj.insert = false;
                        packageObj.validateParam.resetBtn.trigger("reset");
                        updateRow(selectItems[0]);
                    }
                    else {
                        bootbox.alert('请选择一条记录！');
                    }
                }
            }, {
                text: '删除', 'data-culture': 'delete', img: 'fa fa-times', event: function (selectItems) {
                    if (selectItems && selectItems.length > 0) {
                        bootbox.confirm('确认删除？', function () {
                            var ids = [];
                            $.each(selectItems, function (i, row) {
                                ids.push(row.id);
                            });
                            deleteRowList(ids);
                        });
                    } else {
                        bootbox.alert('请选择待删除的记录！');
                    }
                }
            }]
        }
    };

    function updateRow(package) {
        packageObj.currentPackage = package;
        $.setobject(package, 'packageEditForm');
        $('#editmodal').modal('show');
    }
    function deleteRowList(ids) {
        $.request('/workflow/package/delete/ids', {ids: ids}, function () {
            bootbox.alert("删除成功!");
            packageObj.grid.ext.refresh();
        }, function () {
            bootbox.alert("删除失败!");
        })
    }
    function init() {
        $("#grid-data").table(packageObj.grid);
        $('#packageEditForm').validate(packageObj.validateParam);
    }
    function dispose() {
        packageObj = {};
    }
</script>
</body>
</html>